# pygame sprite.py
# author: samuel santos

import pygame
from pygame.locals import*
from sys import exit

import random

#define some color

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

class Block (pygame.sprite.Sprite):

	#constructor
	def __init__(self, color, width, height):
		pygame.sprite.Sprite.__init__(self)
		
		self.image = pygame.Surface([width, height])
		self.image.fill(white)
		self.image.set_colorkey(white)
		pygame.draw.ellipse(self.image, red,[0,0,width, height])

		self.image = pygame.image.load("player.png").convert()
		self.image.set_colorkey(white)
		
		#update the position of this object
		self.rect = self.image.get_rect()
		
	#def update(self):
	#	self.rect.y += 1
	#	
	#	if self.rect.y > screenHeight:
	#		self.resetPos()
	#		#self.game.score -= 1
		
	def moveRight(self):
		self.rect.x += 1
		
	def moveLeft(self):
		self.rect.x -= 1
		
	def moveUp(self):
		self.rect.y -= 1
		
	def moveDown(self):
		self.rect.y += 1
		
	
	def resetPos(self):
		self.rect.y = random.randrange(-100,-10)
		self.rect.x = random.randrange(0,screenWidth)
		
	
		
#initialize pygame
pygame.init()

#set the height and width of the screen	
screenWidth = 600
screenHeight = 400
screen = pygame.display.set_mode([screenWidth, screenHeight])

# this is a list of 'sprites' Each in the program is
# added to this list
# The list is managed by a class called 'RenderPlain'	
blockList = pygame.sprite.RenderPlain()
	
#this is a list of every sprite
# All blocks an the player block as well
allSpriteList = pygame.sprite.RenderPlain()

for i in range(5):
	#this represents a block
	block = Block(black, 20, 15)
	
	#Set a random location for the block
	block.rect.x = random.randrange(screenWidth)
	block.rect.y = random.randrange(screenHeight)

	blockList.add(block)
	allSpriteList.add(block)
	

#create a red player block
player = Block(red, 20, 15)
player.rect.x = screenWidth/2 
player.rect.y = screenHeight/2

allSpriteList.add(player)

clock = pygame.time.Clock()

score = 0

def move(player, dir):

	if dir == "r":
		player.moveRight()
	elif dir == "l":
		player.moveLeft()
	elif dir == "u":
		player.moveUp()
	elif dir == "d":
		player.moveDown()
	

direction ="r"

#loop program
while True:

	move(player, direction)

	#call the update() method
	#blockList.update()
	
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			exit()
			
	pressed_keys = pygame.key.get_pressed()
	
	if pressed_keys[K_RIGHT]:
		direction = "r"
		
	if pressed_keys[K_LEFT]:
		direction = "l"
		
	if pressed_keys[K_UP]:
		direction = "u"
		
	if pressed_keys[K_DOWN]:
		direction = "d"
			
	screen.fill(white)
	
	#Get the current mouse position
	#pos = pygame.mouse.get_pos()
	#player.rect.x = pos[0]
	#player.rect.y = pos[1]
	
	blockHitList = pygame.sprite.spritecollide(player, blockList, True)
	
	#check the list of collisions
	
	if len(blockHitList) > 0:
		score += len(blockHitList)
		print score
		
	#Draw all the sprites
	allSpriteList.draw(screen)
	
	clock.tick(20)
	
	pygame.display.flip()
	